package com.xxh.book.controller;

import com.xxh.book.entity.Admin;
import com.xxh.book.service.AdminService;
import com.xxh.book.service.impl.AdminServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "AdminServlet",urlPatterns = "/admin/*")
public class AdminServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        HttpSession session = request.getSession();

        if(pathInfo.equals("/login")){
            AdminService adminService = new AdminServiceImpl();
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            Admin admin = new Admin();
            admin.setUsername(username);
            admin.setPassword(password);
            Admin ad = adminService.Login(admin);
            if(ad!=null){
                session.setAttribute("user",ad);
                response.sendRedirect(request.getContextPath()+"/index.jsp");

            }else {
                response.sendRedirect(request.getContextPath()+"/login.jsp");
            }
        }

    }
}
